﻿using Quartz;
using Quartz.Impl;
using Quartz.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using static Quartz.MisfireInstruction;

namespace NFine.Web.App_Start._02_Class
{
    public class TimeRun
    {
        public virtual async Task Run()
        {
            ISchedulerFactory sf = new StdSchedulerFactory();
            IScheduler sched = await sf.GetScheduler();                              


            // computer a time that is on the next round minute
            DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow.AddMinutes(1));
            

            // define the job and tie it to our HelloJob class
            IJobDetail job = JobBuilder.Create<TimeJob>()
                .WithIdentity("job1", "group1")
                .Build();

            //// Trigger the job to run on the next round minute
            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("trigger1", "group1")                
                .StartNow()
                .WithCronSchedule("0 15 7 * * ?")
                //.WithCronSchedule("1 * * * * ?")
                //.WithSimpleSchedule(x => x
                //    .WithIntervalInSeconds(40)
                //    .RepeatForever())
                .Build();
            ////.StartAt(runTime)
            ////.Build();

            // CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1", "group1");


            // Tell quartz to schedule the job using our trigger
            await sched.ScheduleJob(job, trigger);
            //IJobDetail job2 = JobBuilder.Create<TimeJob2>()
            //  .WithIdentity("job2", "group1")
            //  .Build();

            //// Trigger the job to run on the next round minute
            //ITrigger trigger2 = TriggerBuilder.Create()
            //    .WithIdentity("trigger2", "group1")
            //   .StartNow()
            //    .WithCronSchedule("*/2 * * * * ?")
            //    .Build();

            //await sched.ScheduleJob(job2, trigger2);
            // Start up the scheduler (nothing can actually run until the
            // scheduler has been started)
            await sched.Start();




        }
    }
}